//
//  L2Array.m
//  Lesson2
//
//  Created by Alexander Trifonov on 20.04.12.
//  Copyright (c) 2012 fgengine@gmail.com. All rights reserved.
//

#import "L2Array.h"

NSInteger sortProc(id a, id b, void *reverse)
{
    if(reverse)
    {
        return [b localizedCaseInsensitiveCompare:a];
    }
    else
    {
        return [a localizedCaseInsensitiveCompare:b];
    }
}

@implementation L2Array

- (void) execute
{
    NSArray *a1 = [NSArray array];
    NSArray *a2 = [NSArray arrayWithArray:a1];
    NSArray *a3 = [NSArray arrayWithObject:@"An object"];
    
    NSDate *aDate = [NSDate date];
    NSNumber *aNamber = [NSNumber numberWithInt:10];
    NSString *aString = @"Bill";
    
    NSLog(@"-L2Array--------------------------------");
    
    NSArray *aArray = [NSArray arrayWithObjects:aDate, aNamber, aString, nil];
    NSLog(@"%@", aArray);
    
    NSLog(@"----------------------------------------");
    
    NSArray *students = [NSArray arrayWithObjects:@"Tom", @"Bill", @"Joe", nil];
    
    BOOL containts = [students containsObject:@"Bill"];
    NSInteger count = [students count];
    id obj = [students lastObject];
    id obj1 = [students objectAtIndex:1];
    
    NSString *results = [NSString stringWithFormat:
                         @"\nstudents contain 'Bill'? %i"
                         @"\nnumber of students is %i"
                         @"\nlast object: %@"
                         @"\nobject at index 1 is %@",
                         containts, count, obj, obj1];
    NSLog(@"%@", results);
    
    NSLog(@"----------------------------------------");
    
    NSInteger location = [students indexOfObject:@"Joe"];
    NSLog(@"Location of Joe is %i", location);
    
    NSLog(@"----------------------------------------");
    
    NSString *joe1 = @"Joe";
    NSString *joe2 = [NSString stringWithFormat:@"%@", @"Joe"];
    NSString *joe3 = [NSString stringWithFormat:@"%@", @"Joe"];
    
    NSArray *a = [NSArray arrayWithObjects:joe1, joe2, joe3, nil];
    NSInteger location1 = [a indexOfObjectIdenticalTo:joe2];
    NSLog(@"%i", location1);
    
    NSLog(@"----------------------------------------");
    
    NSArray *students2 = [NSArray arrayWithObjects:@"Tom", @"Bill", @"Joe", nil];
    BOOL equal = [students2 isEqualToArray:students];
    NSLog(@"Arrays equal? %i", equal);
    
    NSLog(@"----------------------------------------");
    
    NSString *joined = [students componentsJoinedByString:@", "];
    NSLog(@"%@", joined);
    
    NSLog(@"----------------------------------------");
    
    BOOL reverseSort = YES;
    NSArray *sortedArray = [students sortedArrayUsingFunction:sortProc context:(void*)reverseSort];
	NSLog(@"Sort using function:\"YES\" %@", sortedArray);
    
    NSLog(@"----------------------------------------");
    
    NSArray *students3 = [NSArray arrayWithObjects:@"Tom", @"Bill", @"Tom", @"Joe", @"Tom", nil];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains[cd] %@", @"Tom"];
    NSArray *aTom = [students3 filteredArrayUsingPredicate:predicate];
    NSLog(@"%@", aTom);
    
    NSLog(@"----------------------------------------");
}

@end
